home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / _common.js next >
Encoding:
Text File  |  1999-12-01  |  14.3 KB  |  409 lines

  1. //!!!!!!!! NOTICE: THIS FILE IS DEPRECATED IN DREAMWEAVER 3          !!!!!!!!
  2. //!!!!!!!! Please use the common files found in Configuration/Shared !!!!!!!!
  3.  
  4. // Copyright 1998 Macromedia, Inc. All rights reserved.
  5.  
  6. //*************** GLOBALS VARS *****************
  7.  
  8. var REF_UNNAMED = "unnamed <"; //this is what getObjectRefs() returns for unnamed objects;
  9. var REF_CANNOT = "cannot reference <";
  10.  
  11. //**************** GENERIC FUNCTIONS ****************
  12.  
  13. //Given a function call, extracts the args and returns them in array
  14. //Respects ', skips over stuff between quotes, and returns them dequoted.
  15. //IMPORTANT: argArray[0] is the function call!! Actual args start at argArray[1].
  16.  
  17. function extractArgs(behFnCallStr){
  18.   var i, theStr, lastPos, argArray;
  19.  
  20.   argArray = getTokens(behFnCallStr,"(),");
  21.   for (i=0; i<argArray.length; i++) {
  22.     theStr = unescQuotes(argArray[i]);
  23.     lastPos = theStr.length-1;
  24.     if (theStr.charAt(0) == "'" && lastPos > 0 && theStr.charAt(lastPos) == "'")
  25.       argArray[i] = theStr.substring(1,lastPos);
  26.   }
  27.   return argArray
  28. }
  29.  
  30.  
  31.  
  32. //Passed a string, finds special chars '"\ and escapes them with \
  33.  
  34. function escQuotes(theStr){
  35.   var i, theChar, escStr = "";
  36.   for(var i=0; i<theStr.length; i++) {
  37.     theChar = theStr.charAt(i);
  38.     escStr += (theChar=='"' || theChar=="'" || theChar=="\\")?("\\"+theChar):theChar;
  39.   }
  40.   return escStr;
  41. }
  42.  
  43.  
  44.  
  45. //Passed a string, finds any escape chars \ and removes them
  46.  
  47. function unescQuotes(theStr){
  48.   var strLen, i, theChar, unescStr = "";
  49.   strLen = theStr.length;
  50.   for(i=0; i<strLen; i++) {
  51.     theChar = theStr.charAt(i);
  52.     if (theChar == "\\" && i < strLen - 1) //if escape char and not end
  53.       theChar = theStr.charAt(++i); //append next char and skip over
  54.     unescStr += theChar;
  55.   }
  56.   return unescStr;
  57. }
  58.  
  59.  
  60.  
  61. //Invokes dialog to allow user to select filename. Puts value in text input.
  62.  
  63. function browseFile(fieldToStoreURL){
  64.   var fileName = "";
  65.   fileName = browseForFileURL();  //returns a local filename
  66.   if (fileName) fieldToStoreURL.value = fileName;
  67. }
  68.  
  69.  
  70.  
  71. //Given a string "myObject  *" returns "myObject  *".
  72.  
  73. function stripStar(theStr) {
  74.   var endPos;
  75.  
  76.   endPos = theStr.indexOf('  *');
  77.   return ((endPos > 0)? theStr.substring(0,endPos) : theStr);
  78. }
  79.  
  80.  
  81.  
  82. //Given a string "some property (value)" returns "some property".
  83.  
  84. function stripValue(theStr) {
  85.   var endPos = theStr.indexOf(' (');
  86.   return ((endPos > 0)? theStr.substring(0,endPos) : theStr);
  87. }
  88.  
  89.  
  90.  
  91. //Given theSelect obj and an index, it appends a star
  92. //and inserts the new string into the menu at position index.
  93. //If the menu item was "layer[2]" it becomes "layer[2]  *".
  94. //Existing "  *" values get stripped off first.
  95.  
  96. function addStarToMenuItem(theSelect,menuIndex) {
  97.   var newMenuText;
  98.  
  99.   newMenuText = stripStar(theSelect.options[menuIndex].text); //remove if old star
  100.   newMenuText += "  *";  //append "  *"
  101.   theSelect.options[menuIndex]=new Option(newMenuText); //add new line to menu
  102. }
  103.  
  104.  
  105.  
  106. //Given theSelect obj and an index and a value, it appends the value in parens
  107. //and inserts the new string into the menu at position index.
  108. //If the menu item was "layer[2]" and value is "show", it becomes "layer[2] (show)".
  109. //Existing " (value)" values get stripped off first. If value is empty, strips all.
  110.  
  111. function addValueToMenuItem(theSelect,menuIndex,value) {
  112.   var newMenuText = stripValue(theSelect.options[menuIndex].text); //remove old val
  113.   if (value.length > 0) { //if valid value
  114.     newMenuText += " (" + value + ")";  //append " (value)"
  115.   }
  116.   theSelect.options[menuIndex]=new Option(newMenuText); //add new line to menu
  117. }
  118.  
  119.  
  120.  
  121. //Converts an array of JS object references to an array of nice names.
  122. //For example, document.layers['layr1'].document.form1.img1 becomes
  123. //             image "img1" in form "form1" in layer "layr1"
  124. //Assumes all objects are of type objTypeStr, and the last token is the name
  125. //Note: I reverse the array of tokens to simplify nesting.
  126.  
  127. function niceNames(objRefArray,objTypeStr) {
  128.   var i, j, niceRef, tokens;
  129.   var niceNameArray = new Array(objRefArray.length);
  130.  
  131.   for (i in objRefArray) {  //with object reference array
  132.     tokens = getTokens(objRefArray[i],".").reverse();   //split ref into tokens and rev order
  133.     if (tokens.length > 1) {
  134.       niceRef = objTypeStr + ' ' + nameReduce(tokens[0]);  //start building str, ie: image "foo"
  135.       if (tokens.length > 2) {  //reference includes some nesting...
  136.         if (tokens[1] != "document" && tokens[2] == "document") //inside form, add form reference
  137.           niceRef += ' ' + TYPE_Separator + ' ' + TYPE_Form + ' ' + nameReduce(tokens[1]);
  138.         for (j=1; j<tokens.length-1; j++) if (tokens[j].indexOf("layers[") == 0)
  139.             niceRef += ' ' + TYPE_Separator + ' ' + TYPE_Layer + ' ' + nameReduce(tokens[j]);
  140.         if (tokens[j] != "document")  //if top, parent, or window, expect frame
  141.           niceRef += ' ' + TYPE_Separator + ' ' + TYPE_Frame + ' ' + nameReduce(tokens[j-1]);
  142.       }
  143.     } else niceRef = objRefArray[i];
  144.     niceNameArray[i] = niceRef;
  145.   }
  146.   return niceNameArray;
  147. }
  148.  
  149.  
  150.  
  151. //Extracts a name or num from array string and quotes if necessary. So
  152. // myImg         => "myImg"
  153. // layers['foo'] => "foo"
  154. // embeds[0]     => 0
  155. // myImg[2]      => "myImg[2]"
  156.  
  157. function nameReduce (objName) {
  158.   var retVal, arrayTokens;
  159.  
  160.   retVal = '"' + objName + '"';  //default is object wrapped in quotes
  161.   if (objName.indexOf("[") > 0) {  //if it's an array
  162.     arrayTokens = getTokens(objName,"[]\"'");  //break up tokens
  163.     if (arrayTokens.length == 2) {  //if exactly two tokens
  164.       if ("frames layers forms embeds links anchors all".indexOf(arrayTokens[0]) != -1) { //if legal
  165.         if (arrayTokens[1] == ""+parseInt(arrayTokens[1])) //if a number
  166.           retVal = arrayTokens[1];
  167.         else                                               //else it's a string
  168.           retVal = '"' + arrayTokens[1] + '"';
  169.       }
  170.     }
  171.   }
  172.   return retVal;
  173. }
  174.  
  175.  
  176.  
  177. //Emulates printf("blah blah %s blah %s",str1,str2)
  178. //Used for concatenating error message for easier localization.
  179. //Returns assembled string.
  180.  
  181. function errMsg() {
  182. var i,numArgs,errStr="",argNum=0,startPos;
  183.  
  184.   numArgs = errMsg.arguments.length;
  185.   if (numArgs) {
  186.     theStr = errMsg.arguments[argNum++];
  187.     startPos = 0;  endPos = theStr.indexOf("%s",startPos);
  188.     if (endPos == -1) endPos = theStr.length;
  189.     while (startPos < theStr.length) {
  190.       errStr += theStr.substring(startPos,endPos);
  191.       if (argNum < numArgs) errStr += errMsg.arguments[argNum++];
  192.       startPos = endPos+2;  endPos = theStr.indexOf("%s",startPos);
  193.       if (endPos == -1) endPos = theStr.length;
  194.     }
  195.     if (!errStr) errStr = errMsg.arguments[0];
  196.   }
  197.   return errStr;
  198. }
  199.  
  200.  
  201.  
  202. function findObject(objName,  parentObj) {
  203.   var i,tempObj="",found=false,curObj = "";
  204.   var NS = (navigator.appName.indexOf("Netscape") != -1);
  205.   if (!NS && document.all) curObj = document.all[objName]; //IE4
  206.   else {
  207.     parentObj = (parentObj != null)? parentObj.document : document;
  208.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  209.     else { //if in form
  210.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  211.         if (parentObj.forms[i][objName]) {
  212.           curObj = parentObj.forms[i][objName];
  213.           found = true; break;
  214.       } }
  215.       if (!found && NS && parentObj.layers && parentObj.layers.length > 0) {
  216.         parentObj = parentObj.layers;
  217.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  218.           tempObj = findObject(objName,parentObj[i]); //recurse
  219.           if (tempObj) { curObj = tempObj; break;} //if found, done
  220.   } } } }
  221.   return curObj;
  222. }
  223.  
  224.  
  225.  
  226. //Custom non-Javascript code to extract tags and get object names.
  227. //Passed HTML tag (ie IMG), gets the current doc source
  228. //HTML and returns an array of names (empty if unnamed).
  229. //This argument is not case sensitive (can be LAYER, Layer, or layer).
  230. //For Example, given <IMG NAME="myPhoto"> <IMG><IMG name="bobsPhoto">
  231. //returns array: myPhoto,,bobsPhoto
  232.  
  233. function getParam(tagStr,param){
  234.   var j,tokenString;
  235.   var theName = "";
  236.   var tokenArray = new Array;
  237.   tokenArray = getTokens(tagStr," =<>");
  238.   for (j=0; j<(tokenArray.length - 1); j++) {
  239.     tokenString = tokenArray[j].toUpperCase(); //force UPPER CASE
  240.     if (tokenString.indexOf(param.toUpperCase()) == 0) {  //found name
  241.       theName = tokenArray[j+1];  //should return single quoted element in array
  242.       firstChar = theName.charAt(0);
  243.       lastChar = theName.charAt(theName.length - 1);
  244.       if ((firstChar == lastChar) && (firstChar == "'" || firstChar == "\""))
  245.         theName = theName.substring(1,theName.length - 1);
  246.       break;
  247.   } }
  248.   return theName;
  249. }
  250.  
  251. //Passed a string, finds removes special chars '"! and space
  252.  
  253. function badChars(theStr){
  254.   var i,theChar,isBad=false;
  255.   var someBadChars = " ~!@#$%^&*()_+|`-=\\{}[]:\";'<>,./?";
  256.   for (i=0; i<theStr.length; i++) {
  257.     theChar = theStr.charAt(i);
  258.     if (someBadChars.indexOf(theChar) != -1) isBad = true;
  259.   }
  260.   return isBad;
  261. }
  262.  
  263. //Passed a browser type and one or more tags, returns an array of object
  264. //references for the tag(s) and browser type. If the document is in a
  265. //frameset, returns references in all of the frames of the parent frameset.
  266.  
  267. function getAllObjectRefs(browserType,tagName){
  268.   var refsArray = new Array(),theFrame,frameName,Tag,i,j;
  269.   var frameList = getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  270.   var numArgs = arguments.length;
  271.   for (i=1;i<numArgs;i++){
  272.     Tag = arguments[i]
  273.     if (frameList && frameList.length>0) { //if frames
  274.       for (j=0; j<frameList.length; j++) {
  275.         if (frameList[j].indexOf(REF_UNNAMED) != -1)
  276.           theFrame = "parent.frames[" + j + "]";
  277.         else {
  278.           //check for duplicately named frames by checking numer of ['s in name
  279.           //for instance,parent.frames['name'][0] would have 2
  280.           if (frameList[j].indexOf("[")!=frameList[j].lastIndexOf("["))
  281.             frameList[j] = frameList[j].substring(0,frameList[j].indexOf("]") + 1)  
  282.           theFrame = frameList[j];
  283.           //alert("after generating new ref, it is " + theFrame);      
  284.         }  
  285.         refsArray = refsArray.concat(getObjectRefs(browserType,theFrame,Tag));
  286.       }
  287.     } else 
  288.       refsArray = refsArray.concat(getObjectRefs(browserType,"document",Tag)); 
  289.   }
  290.   return refsArray;
  291. }
  292.  
  293. //Passed a tagName, returns an array of all tags of tagName. If the document is in a frameset,
  294. //the array includes all of the tags in all of the frames of the document's parent.
  295.  
  296. function getAllObjectTags(tagName){
  297.   var tagsArray = new Array(),theFrame,frameName,i;
  298.   var frameList = getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  299.   if (frameList && frameList.length>0) { //if frames
  300.     for (i=0;i<frameList.length; i++){
  301.       if (frameList[i].indexOf(REF_UNNAMED)!=-1)
  302.         theFrame = "parent.frames["+i+"]";
  303.       else
  304.         theFrame = frameList[i];
  305.     tagsArray = tagsArray.concat(getObjectTags(theFrame,tagName));  
  306.     }  
  307.   } else
  308.    tagsArray = tagsArray.concat(getObjectTags("document",tagName));    
  309.    return tagsArray;
  310. }
  311.   
  312. //*************** GENERIC DOM MANIPULATION FNS *****************
  313.  
  314. //Returns a function call if exists in event handler.
  315. //  obj       - DOM object, such as dreamweaver.getDocumentDOM().body
  316. //  eventName - "onLoad", "onClick" etc (not case sensitive)
  317. //  fnName    - "MM_preloadImages" etc.
  318. //  optStr    - (optional) function call must contain this string to be found
  319. //Given <TAG onEvent="aaa();bbb();ccc()">,
  320. //calling getHandler(tagObj,'onEvent','bbb') will
  321. //return "bbb()". Returns empty if event or fn don't exist.
  322.  
  323. function getHandler(obj,eventName,fnName, optStr) {
  324.   var eventStr,fnArray,i,theChunk,retVal = "";
  325.   eventStr = obj.getAttribute(eventName);
  326.   if (eventStr) { //find previous call, or add it
  327.     fnArray = dreamweaver.getTokens(eventStr,";");
  328.     for (i=0; i<fnArray.length; i++) { //look at each code chunk
  329.       if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr ||  //fn call found
  330.           fnArray[i].indexOf(optStr) != -1)) {
  331.         retVal = fnArray[i]; break;
  332.     } }
  333.   }
  334.   return retVal
  335. }
  336.  
  337.  
  338.  
  339. //Replaces or adds a fn call to an event handler
  340. //  obj       - DOM object, such as dreamweaver.getDocumentDOM().body
  341. //  eventName - "onLoad", "onClick" etc (not case sensitive)
  342. //  fnCall    - "myFun('arg1','arg2')" etc.
  343. //  optStr    - (optional) function call must contain this string to be found
  344. //Given <TAG onEvent="aaa();bbb();ccc()">,
  345. //calling setHandler(tagObj,'onEvent','bbb(1,2)') will
  346. //replace "bbb()" with the altered fn call. If the event
  347. //does not exist, adds it. It fn didn't exist, adds it to the
  348. //end of the list.
  349.  
  350. function setHandler(obj,eventName,fnCall, optStr) {
  351.   var eventStr,fnName,fnArray=new Array(),i=0;
  352.   eventStr = obj.getAttribute(eventName);
  353.   if (eventStr) { //if event exists
  354.     fnName = fnCall.substring(0,fnCall.indexOf("("));
  355.     fnArray = dreamweaver.getTokens(eventStr,";");
  356.     for (i; i<fnArray.length; i++) //search for fnName
  357.       if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr ||  //fn call found
  358.           fnArray[i].indexOf(optStr) != -1)) break;
  359.   }
  360.   fnArray[i] = fnCall;
  361.   obj.setAttribute(eventName,fnArray.join(";"));
  362.   return true
  363. }
  364.  
  365.  
  366.  
  367. //Deletes a fn call from an event handler
  368. //  obj       - DOM object, such as dreamweaver.getDocumentDOM().body
  369. //  eventName - "onLoad", "onClick" etc (not case sensitive)
  370. //  fnName    - "MM_preloadImages" etc.
  371. //  optStr    - (optional) function call must contain this string to be found
  372. //Given <TAG onEvent="aaa();bbb();ccc()">,
  373. //calling delHandler(tagObj,'onEvent','bbb') will
  374. //remove "bbb();". If it is the last fn in the handler,
  375. //removes the event entirely.
  376.  
  377. function delHandler(obj,eventName,fnName, optStr) {
  378.   var eventStr,fnArray=new Array(),i=0,j;
  379.   eventStr = obj.getAttribute(eventName);
  380.   if (eventStr) { //if event exists
  381.     fnArray = dreamweaver.getTokens(eventStr,";");
  382.     for (i; i<fnArray.length; i++) { //look at each code chunk
  383.       if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr ||  //fn call found
  384.           fnArray[i].indexOf(optStr) != -1)) { //and, if given, optStr exists
  385.         if (fnArray.length == 1) { //if last one, remove attribute
  386.           obj.removeAttribute(eventName);
  387.         } else { //pull out
  388.           for (j=i; j<fnArray.length; j++) fnArray[j] = fnArray[j+1]; //shift array
  389.           fnArray.length--;
  390.           obj.setAttribute(eventName,fnArray.join(';'));
  391.         }
  392.         break;
  393.     } }
  394.   }
  395.   return true
  396. }
  397.  
  398. ///END OF GENERIC DOM MANIPULATION FUNCTIONS
  399.  
  400. //function: getSelectedObj
  401. //description: returns the currently selected object
  402.  
  403. function getSelectedObj(){
  404.    var currSel = dreamweaver.getSelection();
  405.    return dreamweaver.offsetsToNode(currSel[0],currSel[1]);
  406. }
  407.   
  408.  
  409.